package com.fjec.paid.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjec.paid.entity.BizDistribution;
import com.fjec.paid.entity.BizOrderRefund;
import com.fjec.paid.entity.BizOrderRefundLog;
import com.fjec.paid.mapper.BizDistributionMapper;
import com.fjec.paid.schedule.handler.DivideHandler;
import com.fjec.paid.schedule.handler.QueryDivideHandler;
import com.fjec.paid.schedule.handler.QueryRefundHandler;
import com.fjec.paid.schedule.handler.RefundHandler;
import com.fjec.paid.service.IBizDistributionService;
import com.fjec.paid.service.IBizOrderRefundLogService;
import com.fjec.paid.service.IBizOrderRefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/")
public class DivideController {

    @Resource
    private IBizDistributionService bizDistributionService;

    @Resource
    private IBizOrderRefundService bizOrderRefundService;

    @Resource
    private IBizOrderRefundLogService bizOrderRefundLogService;

    @Autowired
    private QueryDivideHandler queryDivideHandler;
    /**
     * market服务调用该接口，落库physical orders分账数据
     * @param ordersToDivide
     * @return
     */
    @PostMapping("/phy/order/divide/save/batch")
    public Integer savePhyDivideBatch(@RequestBody List<Map<String, Object>> ordersToDivide) {
        int result = bizDistributionService.savePhyDivideBatch(ordersToDivide);
        return result;
    }

    /**
     * market服务调用该接口，落库physical orders分账数据
     * @param ordersToDivide
     * @return
     */
    @PostMapping("/serv/order/divide/save/batch")
    public Integer saveServDivideBatch(@RequestBody List<Map<String, Object>> ordersToDivide) {
        int result = bizDistributionService.saveServDivideBatch(ordersToDivide);
        return result;
    }

    @PostMapping("/order/get/dist/info")
    public List<Map<String, Object>> getDistInfosByPaymentIds(@RequestBody List<Long> paymentIds) {
        List<Map<String, Object>> result = bizDistributionService.listMaps(
                new QueryWrapper<BizDistribution>().in("PaymentId", paymentIds));
        return result;
    }

    @PostMapping("/order/get/refund/info")
    public List<Map<String, Object>> getRefundInfosBySubOrderIds(@RequestBody List<Long> subOrderIds) {
        List<Map<String, Object>> result = bizOrderRefundService.listMaps(
                new QueryWrapper<BizOrderRefund>().in("SubOrderId", subOrderIds));
        return result;
    }

    @GetMapping("/order/get/total/account/amount")
    int getTotalAccountAmount(@RequestParam("payeeUserCode") String payeeUserCode) {
        List<BizDistribution> distributeds = bizDistributionService.list(new QueryWrapper<BizDistribution>()
                .eq("PayeeUsercode", payeeUserCode)
                .eq("DistStatus", 2));
        double totalDistributedAmount = distributeds.stream()
                .map(BizDistribution::getShopDivision)
                .mapToDouble(Double::parseDouble)
                .sum();
        Integer totalAmount = (int) (totalDistributedAmount * 100);
        List<String> outTradeNos = distributeds.stream().map(BizDistribution::getOutTradeNo).collect(Collectors.toList());
        List<BizOrderRefundLog> refundLogs = bizOrderRefundLogService.list(new QueryWrapper<BizOrderRefundLog>()
                .in("OutTradeNo", outTradeNos)
                .eq("RefundState", 4));
        int totalRefundAmount = refundLogs.stream()
                .map(BizOrderRefundLog::getRefundAmount)
                .mapToInt(Integer::intValue)
                .sum();
        return totalAmount - totalRefundAmount;
    }

    @PostMapping("/order/ys/deliver")
    int ysDeliver(@RequestParam("paymentId") Long paymentId) {
        int code = bizDistributionService.ysDeliver(paymentId);
        return code;
    }

    @PostMapping("/order/ys/sign")
    int ysSign(@RequestParam("paymentId") Long paymentId) {
        int code = bizDistributionService.ysSign(paymentId);
        return code;
    }

//    @GetMapping("/test/do/divide")
//    public String testDivide() {
//        divideHandler.handle(10, 10);
//        return "success";
//    }

    @Autowired
    private RefundHandler refundHandler;
    @GetMapping("/test/do/refund")
    public String testRefund() {
        refundHandler.handle(10, 0);
        return "success";
    }

    @GetMapping("/test/query/divide")
    public String testQueryDivide() {
        queryDivideHandler.handle(10, 0);
        return "success";
    }

    @Autowired
    private QueryRefundHandler queryRefundHandler;
    @GetMapping("/test/query/refund")
    public String testQueryRefund() {
        queryRefundHandler.handle(10, 0);
        return "success";
    }
}
